package svc

import (
	"context"
	"gozero-microservices/order-service/rpc/internal/config"
	"gozero-microservices/order-service/rpc/internal/model"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type ServiceContext struct {
	Config config.Config
	Logger logx.Logger

	// 注入模型
	OrderModel                model.MallOrderModel
	OrderItemModel            model.MallOrderItemModel
	BarrierModel              model.MallBarrierModel
	OrderDeliveryAddressModel model.MallOrderDeliveryAddressModel
	OrderLogisticsModel       model.MallOrderLogisticsModel
	OrderExpressModel         model.MallOrderExpressModel
}

func NewServiceContext(c config.Config) *ServiceContext {
	// ------------- 获取mysql连接开始 -------------
	datasource := ""
	switch c.Env {
	case "dev":
		datasource = c.MysqlConf.Dev.DataSource
	case "test":
		datasource = c.MysqlConf.Test.DataSource
	case "prod":
		datasource = c.MysqlConf.Prod.DataSource
	}
	sc := sqlx.NewMysql(datasource)
	// ------------- 获取mysql连接结束 -------------

	return &ServiceContext{
		Config: c,
		Logger: logx.WithContext(context.Background()),

		// 创建模型
		OrderModel:                model.NewMallOrderModel(sc),
		OrderItemModel:            model.NewMallOrderItemModel(sc),
		BarrierModel:              model.NewMallBarrierModel(sc),
		OrderDeliveryAddressModel: model.NewMallOrderDeliveryAddressModel(sc),
		OrderLogisticsModel:       model.NewMallOrderLogisticsModel(sc),
		OrderExpressModel:         model.NewMallOrderExpressModel(sc),
	}
}
